#include "AudioSource.h"
#include "AudioSource.h"

void AudioSource::Create()
{
    // Generate a source and buffer
    alGenSources(1, &source);
    alGenBuffers(1, &buffer);
}

void AudioSource::BufferData(int format, const short* data, int size, int freq)
{
    // Fill the buffer with audio data
    alBufferData(buffer, format, data, size, freq);
    // Associate the buffer with the source
    alSourcei(source, AL_BUFFER, buffer);
}

void AudioSource::Play()
{
    // Play the sound
    alSourcePlay(source);
}

void AudioSource::Pause()
{
    alSourcePause(source);
}

void AudioSource::Stop()
{
    alSourceStop(source);

}
void AudioSource::WaitEnd()
{
    while (GetState() == AL_PLAYING);
}

int AudioSource::GetInt(int param)
{
    int res = 0;
    alGetSourcei(source, param, &res);
    return res;
}

int AudioSource::GetState()
{
    return this->GetInt(AL_SOURCE_STATE);
}

float AudioSource::GetFloat(int param)
{
    float res = 0.0f;
    alGetSourcef(source, param, &res);
    return res;
}

void AudioSource::SetInt(int param, int value)
{
    alSourcei(source, param, value);
}

void AudioSource::SetFloat(int param, float value)
{
    alSourcef(source, param, value);

}

float AudioSource::GetGain()
{
    return GetFloat(AL_GAIN);
}

void AudioSource::SetGain(float gain)
{
    SetFloat(AL_GAIN, gain);
}

void AudioSource::SetVec3(int param, float x, float y, float z)
{
    alSource3f(source, param, x, y, z);
}

void AudioSource::GetVec3(int param, float& x, float& y, float& z)
{
    alGetSource3f(source, param, &x, &y, &z);

}

void AudioSource::SetIVec3(int param, int x, int y, int z)
{
    alSource3i(source, param, x, y, z);
}

void AudioSource::GetIVec3(int param, int& x, int& y, int& z)
{
    alGetSource3i(source, param, &x, &y, &z);
}

void AudioSource::Delete()
{
    // Clean up
    alDeleteSources(1, &source);
    alDeleteBuffers(1, &buffer);
};